<?php


namespace App\HttpController\V1;


class OrderCar extends Auth
{

    /**
     * 添加购物车
     * @return bool
     */
    function shoppingAdd(){
        $param=$this->input('id,num');
        foreach($param as $k=>$v) {
            if(empty($v)) return $this->writeJson(-202,null,'参数错误');
        }
        try{
            $data=$this->db
                ->where("user_id={$this->user->id} and goods_id={$param['id']}")
                ->has('shop_car');
            if($data){
                $this->db
                    ->where("user_id={$this->user->id} and goods_id={$param['id']}")
                    ->setInc('shop_car','num',$param['num']);
            }else{
                $insert_date=[
                    'user_id'=>$this->user->id,
                    'goods_id'=>$param['id'],
                    'num'=>$param['num'],
                    'create_time'=>date('Y-m-d H:i:s'),
                ];
                $this->db->insert('shop_car',$insert_date);
            }
            return $this->writeJson(200,null,'添加成功');
        }catch (\Exception $e){
            return $this->writeJson(-201,null,$e->getMessage());
        }

    }

    /**
     * 购物车列表
     * @return bool
     */
    function shoppCarList(){
        $getPage=$this->getPage();
        try{
            $data=$this->db
                ->where('user_id',$this->user->id)
                ->get('shop_car',$getPage,'*');
            //删除已经删除商品
            foreach($data as $k=>$v){
                $goods=$this->db->where('id',$v['goods_id'])->has('goods');
                if(!$goods){
                    $this->db->where('goods_id',$v['goods_id'])->delete('shop_car');
                }
            }
            $userinfo=$this->db->where('id',$this->user->id)->getValue('users','level');
            if(in_array($userinfo,[2,3])){
                $price='vip_price';
            }else{
                $price='market_price';
            }
            $data=$this->db
                ->where('user_id',$this->user->id)
                ->get('shop_car',null,'*');
            foreach($data as $v=>&$k){
                $goodsinfo=$this->db->where('id',$k['goods_id'])->getOne('goods',"goods_name,$price as price,goods_pic");
                $k['goods_name']=$goodsinfo['goods_name'];
                $k['goods_pic']=QINIU_URL.$goodsinfo['goods_pic'];
                $k['price']=$goodsinfo['price'];
            }
            return $this->writeJson(200,$data,'成功');
        }catch (\Exception $e){
            return $this->writeJson(-201,null,$e->getMessage());
        }
    }

    /**
     * 购物车  增加/删除数量
     * @return bool
     */
    function shoppCarEdit(){
        $param=$this->input('id,type');// type 1增加 2删除
        foreach($param as $k=>$v) {
            if(empty($v)) return $this->writeJson(-202,null,'参数错误');
        }
        try{
            if($param['type']==1){
                $this->db->where('id',$param['id'])->setInc('shop_car','num');
            }else{
                $this->db->where('id',$param['id'])->setDec('shop_car','num');
            }
            return $this->writeJson(200,null,'success');
        }catch (\Exception $e){
            return $this->writeJson(-201,null,$e->getMessage());
        }

    }

    /**
     * 删除购物车
     * @return bool
     */
    function shoppCarDel(){
        $id=$this->input('id');
        if(empty($id)) return $this->writeJson(-202,null,'参数错误');
        $arr_id=explode(",",$id);
        try{
                $this->db->whereIn('id',$arr_id)->delete('shop_car');
            return $this->writeJson(200,null,'删除成功');
        }catch (\Exception $e){
            return $this->writeJson(-201,null,$e->getMessage());
        }
    }

}